import { getActivityComments, getAverageScore, getCommentCount, checkUserComment, createComment } from '../../api/comment'

// 评论模块
export default {
  namespaced: true,
  state: {
    // 评论列表
    comments: [],
    // 平均评分
    averageScore: 0,
    // 评论总数
    commentCount: 0,
    // 是否可以评论
    canComment: false,
    // 当前活动ID
    currentActivityId: null,
    // 分页信息
    pagination: {
      currentPage: 1,
      pageSize: 10
    },
    // 加载状态
    loading: false,
    // 错误信息
    error: null
  },
  
  mutations: {
    // 设置评论列表
    SET_COMMENTS(state, comments) {
      state.comments = comments
    },
    
    // 设置平均评分
    SET_AVERAGE_SCORE(state, score) {
      state.averageScore = score
    },
    
    // 设置评论总数
    SET_COMMENT_COUNT(state, count) {
      state.commentCount = count
    },
    
    // 设置是否可以评论
    SET_CAN_COMMENT(state, canComment) {
      state.canComment = canComment
    },
    
    // 设置当前活动ID
    SET_CURRENT_ACTIVITY_ID(state, activityId) {
      state.currentActivityId = activityId
    },
    
    // 设置分页信息
    SET_PAGINATION(state, pagination) {
      state.pagination = { ...state.pagination, ...pagination }
    },
    
    // 设置加载状态
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    
    // 设置错误信息
    SET_ERROR(state, error) {
      state.error = error
    },
    
    // 添加新评论
    ADD_COMMENT(state, comment) {
      state.comments.unshift(comment)
      state.commentCount++
    }
  },
  
  actions: {
    // 获取活动评论列表
    async getActivityComments({ commit, rootDispatch }, params) {
      try {
        commit('SET_CURRENT_ACTIVITY_ID', params.activityId)
        commit('SET_PAGINATION', {
          currentPage: params.pageNum || 1,
          pageSize: params.pageSize || 10
        })
        const response = await getActivityComments(params)
        commit('SET_COMMENTS', response.data.records || [])
        return response.data.records || []
      } catch (error) {
        const errorMsg = error.response?.data?.message || '获取评论列表失败'
        commit('SET_ERROR', errorMsg)
        rootDispatch('showError', errorMsg)
        return []
      }
    },
    
    // 获取平均评分
    async getAverageScore({ commit, rootDispatch }, activityId) {
      try {
        const response = await getAverageScore(activityId)
        const score = response.data || 0
        commit('SET_AVERAGE_SCORE', score)
        return score
      } catch (error) {
        console.error('获取平均评分失败:', error)
        return 0
      }
    },
    
    // 获取评论数量
    async getCommentCount({ commit, rootDispatch }, activityId) {
      try {
        const response = await getCommentCount(activityId)
        const count = response.data || 0
        commit('SET_COMMENT_COUNT', count)
        return count
      } catch (error) {
        console.error('获取评论数量失败:', error)
        return 0
      }
    },
    
    // 检查用户是否可以评论
    async checkUserComment({ commit, rootDispatch }, activityId) {
      try {
        const response = await checkUserComment(activityId)
        const canComment = response.data.canComment || false
        commit('SET_CAN_COMMENT', canComment)
        return canComment
      } catch (error) {
        console.error('检查评论权限失败:', error)
        return false
      }
    },
    
    // 提交评论
    async createComment({ commit, rootDispatch }, commentData) {
      try {
        rootDispatch('showLoading')
        const response = await createComment(commentData)
        commit('ADD_COMMENT', response.data)
        commit('SET_CAN_COMMENT', false)
        rootDispatch('showSuccess', '评价成功')
        return response.data
      } catch (error) {
        const errorMsg = error.response?.data?.message || '评价失败'
        rootDispatch('showError', errorMsg)
        throw error
      } finally {
        rootDispatch('hideLoading')
      }
    },
    
    // 加载活动的评论相关信息
    async loadCommentInfo({ dispatch, rootDispatch }, activityId) {
      try {
        rootDispatch('showLoading')
        // 并行获取评论相关信息
        const [canComment, averageScore, commentCount] = await Promise.all([
          dispatch('checkUserComment', activityId),
          dispatch('getAverageScore', activityId),
          dispatch('getCommentCount', activityId)
        ])
        return {
          canComment,
          averageScore,
          commentCount
        }
      } catch (error) {
        console.error('加载评论信息失败:', error)
        return {
          canComment: false,
          averageScore: 0,
          commentCount: 0
        }
      } finally {
        rootDispatch('hideLoading')
      }
    },
    
    // 清空评论数据
    clearComments({ commit }) {
      commit('SET_COMMENTS', [])
      commit('SET_AVERAGE_SCORE', 0)
      commit('SET_COMMENT_COUNT', 0)
      commit('SET_CAN_COMMENT', false)
      commit('SET_CURRENT_ACTIVITY_ID', null)
    }
  },
  
  getters: {
    // 获取评论列表
    comments: state => state.comments,
    
    // 获取平均评分
    averageScore: state => state.averageScore,
    
    // 获取评论总数
    commentCount: state => state.commentCount,
    
    // 获取是否可以评论
    canComment: state => state.canComment,
    
    // 获取当前活动ID
    currentActivityId: state => state.currentActivityId,
    
    // 获取分页信息
    pagination: state => state.pagination,
    
    // 获取加载状态
    isLoading: state => state.loading,
    
    // 获取错误信息
    error: state => state.error
  }
}